export const postMessage = () => {
  // 服务器端没有window对象
  if (process.client) {
    let obj = {};
    window.addEventListener("message", e => {
      let d = {};
      try {
        d = JSON.parse(e.data);
        if (d && d.userInfo) {
          let u = d.userInfo;
          localStorage.setItem("token", u.ssoToken);
          localStorage.setItem("userInfo", JSON.stringify(d.userInfo));
          obj.type = 2; // 2代表登录
          window.parent.postMessage(JSON.stringify(obj), "*");
        } else {
          localStorage.removeItem("token");
          sessionStorage.removeItem("token");
          obj.type = -2; // -2代表登出
          window.parent.postMessage(JSON.stringify(obj), "*");
        }
      } catch (e) {
      }
    });
  }
};
// 登录
export const sso_login = () => {
  if (process.client) {
    let url = r => {
      let returnUrl = encodeURIComponent(window.location.href);
      return `https://${r}sso.caigou2003.com/#/login?returnUrl=${returnUrl}`;
    };
    let _ = url(""); // 正式
    if (window.location.host.indexOf("test") !== -1) _ = url("test"); // 测试
    window.location.href = _;
  }
};
// 注册
export const sso_register = () => {
  if (process.client) {
    let url = r => {
      let returnUrl = encodeURIComponent(window.location.href);
      return `https://${r}sso.caigou2003.com/#/login?returnUrl=${returnUrl}&tab=2`;
    };
    let _ = url(""); // 正式
    if (window.location.host.indexOf("test") !== -1) _ = url("test"); // 测试
    window.location.href = _;
  }
};
// 登出
export const sso_signOut = () => {
  if (process.client) {
    let url = r => {
      let returnUrl = encodeURIComponent(window.location.origin);
      return `https://${r}sso.caigou2003.com/#/signOut?returnUrl=${returnUrl}`;
    };
    let _ = url(""); // 正式
    if (window.location.host.indexOf("test") !== -1) _ = url("test"); // 测试
    window.location.href = _;
  }
};
// 修改密码
export const sso_modifyPwd = () => {
  if (process.client) {
    let url = r => {
      let returnUrl = encodeURIComponent(window.location.href);
      return `https://${r}sso.caigou2003.com/#/modifyPwd?returnUrl=${returnUrl}`;
    };
    let _ = url(""); // 正式
    if (window.location.host.indexOf("test") !== -1) _ = url("test"); // 测试
    window.location.href = _;
  }
}
